package com.jsh.erp.controller;

import com.jsh.erp.datasource.entities.SysCityCost;
import com.jsh.erp.datasource.mappers.SysCityCostMapper;
import com.jsh.erp.service.CityCostService;
import com.jsh.erp.utils.BaseResponseInfo;
import lombok.Lombok;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("/city")
public class SysCityCostController {

    @Resource
    private CityCostService cityCostService;

    @Resource
    private SysCityCostMapper cityCostMapper;

    @GetMapping("/tree")
    public BaseResponseInfo getCityTree() {
        BaseResponseInfo res = new BaseResponseInfo();
        res.code = 200;
        res.data = cityCostService.getCityTree();
        return res;
    }

    @GetMapping("/province")
    public BaseResponseInfo getProvince(){
        BaseResponseInfo res = new BaseResponseInfo();
        res.code = 200;
        res.data = cityCostMapper.findCityByType(1);
        return res;
    }

    @GetMapping("/child/{id}")
    public BaseResponseInfo getChildren(@PathVariable Long id) {
        BaseResponseInfo res = new BaseResponseInfo();
        res.code = 200;
        res.data = cityCostMapper.findCityByParent(id);
        return res;
    }

    @PostMapping("/edit")
    public BaseResponseInfo updateCost(@RequestBody SysCityCost sysCityCost) {
        BaseResponseInfo res = new BaseResponseInfo();
        res.code = 200;
        res.data = cityCostMapper.updateCityCost(sysCityCost);
        return res;
    }

    @GetMapping("/getInfo/{id}")
    public BaseResponseInfo getInfo(@PathVariable("id") Long id) {
        SysCityCost sysCityCost = cityCostMapper.findById(id);
        BaseResponseInfo res = new BaseResponseInfo();
        res.data = sysCityCost;
        res.code = 200;
        return res;
    }

    @GetMapping("/getCityName/{id}")
    public BaseResponseInfo getCityName(@PathVariable("id") Long id) {
        SysCityCost city = cityCostMapper.findById(id);
        SysCityCost province = cityCostMapper.findById(city.getParentId());
        String cityName = province.getName() + city.getName();
        BaseResponseInfo res = new BaseResponseInfo();
        res.data = cityName;
        return res;
    }

    @GetMapping("/getCost/{id}/{type}")
    public BaseResponseInfo getCost(@PathVariable("id") Long id, @PathVariable("type") String type) {
        SysCityCost sysCityCost = cityCostMapper.findById(id);
        BaseResponseInfo res = new BaseResponseInfo();
        res.code = 200;
        if ("car".equals(type)) {
            res.data = sysCityCost.getCarCost();
        } else if ("ship".equals(type)) {
            res.data = sysCityCost.getShipCost();
        } else if ("train".equals(type)) {
            res.data = sysCityCost.getTrainCost();
        } else {
            res.data = 0;
        }
        return res;
    }
}
